package com.example.administrator.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.example.administrator.bean.AlbumImage;
import com.example.administrator.impl.OnCompatCompoundCheckListener;
import com.example.administrator.impl.OnCompatItemClickListener;
import com.example.administrator.mypictureshow.R;
import com.example.administrator.utils.DisplayUtils;
import com.example.administrator.utils.ImageLocalLoader;

import java.util.List;

/**
 * 相册图库适配器
 * Created by chenfeifei on 2018\7\30 0030.
 */

public class AlbumContentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<AlbumImage> mAlbumImages;
    private Context mContext;

    private OnCompatItemClickListener mItemClickListener;

    private OnCompatCompoundCheckListener mOnCompatCheckListener;

    public AlbumContentAdapter(Context mContext) {
        this.mContext = mContext;
    }
    //条目点击事件
    public void setItemClickListener(OnCompatItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;
    }
    //选择框选择事件
    public void setOnCheckListener(OnCompatCompoundCheckListener checkListener) {
        this.mOnCompatCheckListener = checkListener;
    }

    private static int size = (DisplayUtils.screenWidth - 6) / 3;
    //刷新数据
    public void notifyDataSetChanged(List<AlbumImage> albumImages) {
        this.mAlbumImages = albumImages;
        super.notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mAlbumImages == null? 0 : mAlbumImages.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View holderView = LayoutInflater.from(mContext).inflate(R.layout.album_item_album_content_image,parent,false);
        return new GalleryContentImageHolder(holderView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        AlbumImage albumImage = mAlbumImages.get(holder.getAdapterPosition());
        GalleryContentImageHolder imageHolder = (GalleryContentImageHolder) holder;
//        imageHolder.setButtonTint(mColorStateList);
        imageHolder.setData(albumImage);
        imageHolder.setItemClickListener(mItemClickListener);
        imageHolder.setOnCompatCheckListener(mOnCompatCheckListener);
    }

    private static class GalleryContentImageHolder extends RecyclerView.ViewHolder implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

        private ImageView mIvImage;
        private AppCompatCheckBox mCompatCheckBox;

        private OnCompatItemClickListener mItemClickListener;
        private OnCompatCompoundCheckListener mOnCompatCheckListener;

        public GalleryContentImageHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            itemView.getLayoutParams().width = size;
            itemView.getLayoutParams().height = size;
            itemView.requestLayout();

            mIvImage = (ImageView) itemView.findViewById(R.id.iv_album_content_image);
            mCompatCheckBox = (AppCompatCheckBox) itemView.findViewById(R.id.cb_album_check);
            mCompatCheckBox.setOnCheckedChangeListener(this);
        }

        @SuppressLint("RestrictedApi")
        public void setButtonTint(ColorStateList colorStateList) {
            mCompatCheckBox.setSupportButtonTintList(colorStateList);
        }

        public void setData(AlbumImage albumImage) {
            ImageLocalLoader.getInstance().loadImage(mIvImage, albumImage.getPath(), size, size);
            mCompatCheckBox.setChecked(albumImage.isChecked());
        }

        public void setItemClickListener(OnCompatItemClickListener mItemClickListener) {
            this.mItemClickListener = mItemClickListener;
        }

        public void setOnCompatCheckListener(OnCompatCompoundCheckListener mOnCompatCheckListener) {
            this.mOnCompatCheckListener = mOnCompatCheckListener;
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null)
                mItemClickListener.onItemClick(v, getAdapterPosition());
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (mOnCompatCheckListener != null)
                mOnCompatCheckListener.onCheck(buttonView, getAdapterPosition(), isChecked);
        }
    }



}
